package photogcalc.calculators;

import photogcalc.calc.AngleOfView;
import photogcalc.util.Formatter;
import photogcalc.util.Settings;
import javax.microedition.lcdui.Screen;
import net.sf.microlog.core.Logger;
import net.sf.microlog.core.LoggerFactory;
import photogcalc.ChildFormListener;
import photogcalc.common.FrameSize;

/**
 *
 * @author aokkonen
 */
public class AovController extends CalculatorController implements DofCommandListener {
    private AovView view = null;
    private AovModel model = null;

    Logger logger = LoggerFactory.getLogger(getClass().getName());

    public AovController(ChildFormListener listener, Settings settings) {
        super(listener);

        model = new AovModel(settings);
        view = new AovView(settings, this);

        view.initData(model.getCameraFormats(), model.getFlengths());
    }

    public void calculateCommand() {
        logger.debug("calculateCommand");
        calculateAov();
    }

    private void calculateAov() {
        // Calculate angle of view.
        FrameSize frameSize = (FrameSize) model.getFrameSizes().get(model.getSettings().get("cameraformat"));
        AngleOfView aov = new AngleOfView(
                frameSize.getWidth(),
                frameSize.getHeight()
        );

        view.setAovHoriz(Formatter.format(aov.calculateHorizontalAngle(Double.parseDouble(model.getSettings().get("focallength"))), 2)+"\u00B0");
        view.setAovVert(Formatter.format(aov.calculateVerticalAngle(Double.parseDouble(model.getSettings().get("focallength"))), 2)+"\u00B0");
        view.setAovDiag(Formatter.format(aov.calculateDiagonalAngle(Double.parseDouble(model.getSettings().get("focallength"))), 2)+"\u00B0");
        
        double w = aov.calculateWidth(
                (Double.parseDouble(model.getSettings().get("distance"))),
                Double.parseDouble(model.getSettings().get("focallength")));
        double h = aov.calculateHeight(
                (Double.parseDouble(model.getSettings().get("distance"))),
                Double.parseDouble(model.getSettings().get("focallength")));

        String wStr = (w < 1.0) ? Formatter.format(w * 100, 1)+"cm" : Formatter.format(w, 1)+"m";
        String hStr = (h < 1.0) ? Formatter.format(h * 100, 1)+"cm" : Formatter.format(h, 1)+"m";

        view.setSize(wStr+" * "+hStr);

        aov = null;
    }

    public String getIconPrefix() {
        return AovView.ICONPREFIX;
    }

    public String getMenuName() {
        return AovView.MENUNAME;
    }

    public void refreshScreen() {
        view.refresh();
    }

    public Screen getScreen() {
        return view.getScreen();
    }
}
